package org.bai.algorithm.day02;

import org.bai.algorithm.entity.ListNode;
import org.bai.algorithm.util.ListNodeUtil;
import org.bai.algorithm.util.RevealUtil;

/**
 * @ClassName Solution
 * @Description <a href="https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/">从尾到头打印链表</a>
 * @Author bai
 * @Date 2021/8/10 23:37
 * @Version 1.0
 **/
public class ReversePrint {

    public static int[] reversePrint(ListNode<Integer> head) {
        ListNode<Integer> temporaryHeadNode = new ListNode<>();
        ListNode<Integer> temporaryNode;
        int size = 0;

        while (head != null) {
            temporaryNode = head;
            head = head.next;

            temporaryNode.next = temporaryHeadNode.next;
            temporaryHeadNode.next = temporaryNode;
            ++size;
        }

        return listToArray(temporaryHeadNode.next, size);
    }

    public static int[] listToArray(ListNode<Integer> head, int size) {
        int[] temporartArray = new int[size];
        int index = 0;
        while (head != null) {
            temporartArray[index++] = head.val;
            head = head.next;
        }
        return temporartArray;
    }

    public static void main(String[] args) {
        ListNode<Integer> headNode = ListNodeUtil.buildInt(10, 100);
        RevealUtil.reveal(headNode);
        RevealUtil.reveal(reversePrint(headNode));
    }
}
